package com.fenghuajueli.libbasecoreui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.fenghuajueli.libbasecoreui.R;

/**
 * 空提示的view
 */
public class BaseTipsView extends RelativeLayout {
    private View contentView;
    private Button btnAction;
    //提示的标题
    private TextView tvTipsTitle;
    //提示的说明
    private TextView tvTipsDesc;
    private ImageView noDataImg;
    private Context mContext;
    private String tipsTitle = "";
    private String tipsDesc = "";
    private String btnName = "";
    private OnClickListener onClickListener;
    private boolean isShowIcon = false;
    private int imgId = R.mipmap.aa_icon_nodataclip;


    public BaseTipsView(Context context) {
        this(context, null);
    }

    public BaseTipsView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseTipsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseTipsView);
        tipsTitle = typedArray.getString(R.styleable.BaseTipsView_bt_tips_title);
        tipsDesc = typedArray.getString(R.styleable.BaseTipsView_bt_tips_desc);
        btnName = typedArray.getString(R.styleable.BaseTipsView_bt_tips_btn_name);
        isShowIcon = typedArray.getBoolean(R.styleable.BaseTipsView_bt_show_img, false);
        imgId = typedArray.getResourceId(R.styleable.BaseTipsView_bt_tips_img_src, R.mipmap.aa_icon_nodataclip);
        typedArray.recycle();
        init();
    }

    private void init() {
        contentView = View.inflate(mContext, R.layout.base_no_content_tip_view_layout, this);
        btnAction = contentView.findViewById(R.id.btnAction);
        tvTipsTitle = contentView.findViewById(R.id.tvTipsTitle);
        tvTipsDesc = contentView.findViewById(R.id.tvTipsContent);
        noDataImg = contentView.findViewById(R.id.noDataImg);
        if (!TextUtils.isEmpty(tipsTitle)) {
            tvTipsTitle.setText(tipsTitle);
        } else {
            tvTipsTitle.setText("");
        }
        if (!TextUtils.isEmpty(tipsDesc)) {
            tvTipsDesc.setText(tipsDesc);
        } else {
            tvTipsDesc.setText("");
        }
        if (isShowIcon) {
            noDataImg.setVisibility(VISIBLE);
            noDataImg.setImageResource(imgId);
        } else {
            noDataImg.setVisibility(GONE);
        }
        if (!TextUtils.isEmpty(btnName)) {
            btnAction.setText(btnName);
        } else {
            btnAction.setText("");
        }
        btnAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(v);
                }
            }
        });
    }


    public void setActionListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

}
